package com.hsmw.api.controller.biz;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hsmw.api.service.*;
import com.hsmw.api.vo.ConcreteCountTypeVO;
import com.hsmw.api.vo.RetailerOrForemanInfoVO;
import com.hsmw.common.base.BaseController;
import com.hsmw.common.base.BasePage;
import com.htn.common.core.base.DataResponse;
import com.htn.common.core.constant.CommonConstants;
import com.htn.common.core.utils.StringUtils;
import com.hsmw.domain.HsmwAudit;
import com.hsmw.domain.HsmwHtnUser;
import com.hsmw.domain.HsmwUser;
import com.hsmw.domain.form.GetObjectByIdForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;

/**
 * @author WD
 */
@Api(tags = "BIZ-散户审核管理")
@RestController
@RequestMapping("/api/retail")
public class BizRetailController extends BaseController<HsmwAuditService, HsmwAudit> {




	@Autowired
	private HsmwVehicleService hsmwVehicleService;


	@Autowired
	private HsmwLeasingCompanyService hsmwLeasingCompanyService;


	@Autowired
	private HsmwConcretePlantInfoService hsmwConcretePlantInfoService;

	@Autowired
	private HsmwPmUserService hsmwPmUserService;

	@Autowired
	private HsmwConcreteUserService hsmwConcreteUserService;

	@Autowired
	private HsmwDriverUserService hsmwDriverUserService;

	@Autowired
	private HsmwBizUserService hsmwBizUserService;

	@Autowired
	private HsmwHtnUserService hsmwHtnUserService;

	@Autowired
	private HsmwTpspUserService hsmwTpspUserService;

	@Autowired
	private HsmwSaleUserService hsmwSaleUserService;

	/**
	 * 分页
	 *
	 * @param basePage
	 * @return
	 */
	@ApiOperation(value = "获取散户分页列表，参数： 审核状态 auditStatus 1: 审核中、2: 审核通过、3: 审核拒绝 ；姓名或者电话：queryParam；提交时间：createTime  格式：yyyy-MM-dd HH:mm:ss  ", notes = "获取司机分页列表", produces = "application/json")
	@PostMapping("/getHsmwRetailPage")
	public DataResponse getHsmwRetailPage(@RequestBody @Valid BasePage<HsmwHtnUser> basePage) {
		IPage page = new Page(basePage.getPageNo(), basePage.getPageSize());
		HsmwHtnUser hsmwUser = basePage.getRequestData();
		IPage<HsmwHtnUser> data = hsmwHtnUserService.getRetailAuditPageInfo(page, hsmwUser);
		return DataResponse.success(data);
	}

	/**
	 * 分页
	 *
	 * @param basePage
	 * @return
	 */
	@ApiOperation(value = "获取审核通过的 散户分页列表，参数： 审核状态 auditStatus 1: 审核中、2: 审核通过、3: 审核拒绝 ；姓名或者电话：queryParam；提交时间：createTime  格式：yyyy-MM-dd HH:mm:ss  ", notes = "获取司机分页列表", produces = "application/json")
	@PostMapping("/getPassHsmwRetailPage")
	public DataResponse getPassHsmwRetailPage(@RequestBody @Valid BasePage<HsmwHtnUser> basePage) {
		IPage page = new Page(basePage.getPageNo(), basePage.getPageSize());
		HsmwHtnUser hsmwUser = basePage.getRequestData();
		IPage<HsmwHtnUser> data = hsmwHtnUserService.getPassRetailAuditPageInfo(page, hsmwUser);
		return DataResponse.success(data);
	}

	/**
	 * 根据id查询散户数据
	 *
	 * @param form
	 * @return
	 */
	@ApiOperation(value = "根据id查询散户数据", notes = "根据id查询项目数据", produces = "application/json")
	@PostMapping("/getHsmwItemById")
	public DataResponse getHsmwItemById(@RequestBody GetObjectByIdForm form) {
		HsmwHtnUser hsmwUser = hsmwHtnUserService.getHsmwUserById(form.getKey());
		return new DataResponse(hsmwUser);
	}

	/**
	 * 修改散户审核信息
	 * @param hsmwUser
	 * @return
	 */
	@ApiOperation(value = "中控端 修改散户审核信息", notes = "修改散户审核信息", produces = "application/json")
	@PostMapping("/upadteItemInfo")
	public DataResponse upadteItemInfo(@RequestBody HsmwHtnUser hsmwUser) {
		try {
			hsmwHtnUserService.updateById(hsmwUser);
			return new DataResponse();
		}catch (Exception e){
			return  DataResponse.failure(CommonConstants.ResponseStatus.VALIDATE_FAILED.getCode(),"散户审核信息修改失败，请稍后重试！");
		}
	}

	/**
	 * @param form
	 * @return
	 */
	@ApiOperation(value = "散户审核通过", notes = "审核通过", produces = "application/json")
	@PostMapping("/passHsmwRetail")
	public DataResponse passHsmwRetail(@RequestBody GetObjectByIdForm form) {
		hsmwHtnUserService.passHsmwRetail(form.getKey(),form.getRemark(),form.getOwnCompanyName());
		return new DataResponse(true);
	}

	/**
	 * @param form
	 * @return
	 */
	@ApiOperation(value = "散户 审核拒绝", notes = "审核拒绝  id、remark：备注", produces = "application/json")
	@PostMapping("/refuseHsmwAudit")
	public DataResponse refuseHsmwAudit(@RequestBody JSONObject form) {
		hsmwHtnUserService.refuseHsmwRetail(form.getStr("key"), form.getStr("remark"));
		return new DataResponse(true);
	}



	/**
	 * @param basePage
	 * @return
	 */
	@ApiOperation(value = "中控 管理 获取散户的列表", notes = "获取散户的列表", produces = "application/json")
	@ApiImplicitParam(value = "queryParam" ,name = "查询关键字")
	@PostMapping("/getRetailerList")
	public DataResponse getRetailerList(@RequestBody @Valid BasePage basePage) {
		HashMap param = new HashMap();
		IPage<RetailerOrForemanInfoVO> retailerUserInfoVOS = hsmwHtnUserService.getRetailerList(basePage);
		return new DataResponse(retailerUserInfoVOS);
	}




	/*
	 * 分页
	 * @param basePage
	 * @return
	 */
	@ApiOperation(value = "获取散户或者工长的 按照月统计 数据 ", notes = "获取司机分页列表", produces = "application/json")
	@ApiImplicitParam(name = "type",value = "统计纬度 月 month  年 year",dataType = "String")
	@PostMapping("/getConcreteStatistics")
	public DataResponse getConcreteStatistics(@RequestBody ConcreteCountTypeVO concreteCountTypeVO) {
		if(StringUtils.isBlank(concreteCountTypeVO.getMonth())){
			return  new DataResponse(CommonConstants.ResponseStatus.VALIDATE_FAILED,"您输入的月份为空！");
		}
		HashMap data = hsmwConcretePlantInfoService.getItemStatistics(concreteCountTypeVO);
		return DataResponse.success(data);
	}








}
